import Vue from 'vue'
import VueRouter from 'vue-router'


import LoginView from "@/user/LoginView.vue";
import MainView from "@/user/MainView.vue";
import WelcomeView from "@/user/WelcomeView.vue";
import {Message} from "element-ui";
import AccountView from "@/user/AccountView.vue";
import FlowerCommentView from "@/user/FlowerCommentView.vue";
import RoleView from "@/user/RoleView.vue";
import FlowerTypeView from "@/user/FlowerTypeView.vue";
import OrderView from "@/user/OrderView.vue";


Vue.use(VueRouter)

const routes = [
    {path: '/', name: 'LoginView', component: LoginView},
    {
        path: '/main',
        name: 'MainView',
        component: MainView,
        children: [
            {path: '/welcome', name: 'WelcomeView', component: WelcomeView},
            {path: '/account/select', name: 'AccountView', component: AccountView},
            {path: '/comment/select', name: 'FlowerCommentView', component:FlowerCommentView},
            {path: '/role/select', name: 'RoleView', component:RoleView},
            {path: '/type/select', name: 'FlowerTypeView', component:FlowerTypeView},
            {path: '/order/select', name: 'OrderView', component:OrderView},
        ]
    }
]

const router = new VueRouter({
    routes
})


router.beforeEach(async (to, from, next) => {
    let token = sessionStorage.getItem("token");
    console.log(token)
    console.log("上一个视图名：" + from)
    console.log("要访问的视图名：" + to.name)
    if (token === null && to.name !== 'LoginView') {
        console.log("------前端守卫，没有登录拦截-------")
        Message({
            type: "error",
            message: "错了哦，请正确登录",
            duration: 1000
        })
        //将用户重定向到登录页面
        return {name: 'LoginView'}
    }
    next();
})

export default router
